package VolatileDemo;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @projectName: JUC
 * @package: VolatileDemo
 * @className: VolatileTest2
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 * @date: 2022/3/4 16:43
 * @version: 1.0
 */
public class VolatileTest2 {
    private volatile static AtomicInteger num = new AtomicInteger(0);

    public static void add() {
        //num++;
        num.getAndIncrement();
    }

    public static void main(String[] args) {
        for (int i = 1; i <= 20; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    add();
                }
            }).start();
        }
        while (Thread.activeCount() > 2) {
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName() + num);
    }
}
